package com.espirit.init.base;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.espirit.eap.manager.PartyBuilderSupport;
import com.espirit.eap.pagelayout.DetailLayout;
import com.espirit.eap.pagelayout.FlatSection;
import com.espirit.eap.pagelayout.FunctionButton;
import com.espirit.eap.pagelayout.GridSection;
import com.espirit.eap.pagelayout.ModelField;
import com.espirit.eap.sa.LoginUser;
import com.espirit.eap.sa.LoginUserService;
import com.espirit.eap.sa.User;
import com.espirit.eap.sa.UserService;
import com.espirit.eap.sa.WebsiteService;
import com.googlecode.cswish.annotation.Dependence;
@Component
@Dependence({"com.espirit.init.base.LoginUserBuilder"})
public class UserBuilder extends PartyBuilderSupport {
	private static final String FIELD_EMPLOYEE="employee";
	
	private static final String FIELD_PASSWORD="loginUser.password";
	private static final String FIELD_PASSWORD1="password1";
	private static final String FIELD_PASSWORD2="password2";
	
	private static final String LAYOUT_CHANGEPASSWORD = "changePassword";
	
	private FunctionButton apply = null;
	
	@Resource
	private UserService userService;
	
	@Resource
	private LoginUserService loginUserService;
	
	@Resource
	private WebsiteService websiteService;
	
	FunctionButton changePassword = null;
	FunctionButton preChangePassword = null;
	@Override
	public boolean init() {
		super.init();
		return super.setClass(User.class, "User", "用户") ;
	}
	
	@Override
	public void buildFields() {
		super.buildFields();
		addModelField(FIELD_EMPLOYEE, "Employee", "职员");
		
		addModelField(FIELD_PASSWORD, "Password", "密码");
		addModelField(FIELD_PASSWORD1, "Password", "新密码");
		addModelField(FIELD_PASSWORD2, "Password", "新密码(确认)");
	}
	
	@Override
	public void prepareLayoutElements() {
		preChangePassword = super.createFunctionButton("_changePassword", "ChangePassword", "修改密码", User.class, "_changePassword", true);
		changePassword = super.createFunctionButton("changePassword", "ChangePassword", "修改密码", User.class, "changePassword", true);
		ModelField mfCode = getModelField(CODE);
		ModelField mfName = getModelField(NAME);
		ModelField mfEmployee = getModelField(FIELD_EMPLOYEE);

		ModelField mfPassword = getModelField(FIELD_PASSWORD);
		ModelField mfDescription = getModelField(DESCRIPTION);

		addSearchField(mfCode,mfName);
		addListField(mfCode,mfName,mfEmployee,mfDescription);
		addDetailField(mfCode,mfName,mfPassword,mfEmployee,mfDescription );
	}
	
	
	@Override
	protected ModelField addModelFieldCode(String enName, String cnName) {
		ModelField mf = super.addModelFieldCode("Account", "帐号");
		return mf;
	}

	@Override
	protected void addModelFieldName(String enName, String cnName) {
		super.addModelFieldName("Name", "姓名");
	}
	
	@Override
	protected void afterBuilderLayout() {
		//build chnage password layout
		DetailLayout ll = createDetailLayout(model, LAYOUT_CHANGEPASSWORD, true);
		ll.setDefaulted(false);
		FlatSection fs = ll.createFlatSection();
		fs.setColsPerRow(1);
		elementService.addSectionElement(fs, convertFieldToElement(getModelField(FIELD_PASSWORD1), false));
		elementService.addSectionElement(fs, convertFieldToElement(getModelField(FIELD_PASSWORD2), false));

		apply = super.createFunctionButton("apply", "Apply", "Apply", User.class, "apply", true);
		updateObject(apply);
		elementService.addSectionElement(getDefaultDetailLayout().createFlatSection(), apply);
		
		super.afterBuilderLayout();
		
		
		GridSection gridSection = model.getDefaultListLayout().getMainGridSection();
		setListElementLength(gridSection, CODE, 80);
		setListElementLength(gridSection, NAME, 80);
		setListElementLength(gridSection, DESCRIPTION, 120);
		
		updateObject(model);
	}
	
	@Override
	public void initDefaultData() {
		super.initDefaultData();
		
		User admin = new User();
		admin.setCode("admin");
		admin.setName("admin");
		LoginUser adminLoginUser = new LoginUser();
		adminLoginUser.setName("admin");
		adminLoginUser.setPassword(loginUserService.getEncryptPassword("admin"));
		admin.setLoginUser(adminLoginUser);
		addModel(admin);
		
	}
}
